#include <iostream>
#include <memory>
#include <utility>

class Bed {
private:
    std::string name;

public:
    Bed() { std::cout << "Bed created2"<<std::endl; name="default"; }

    ~Bed() { std::cout << "Bed destroyed2"<<std::endl; }

    std::string get_name(){
        return  name;
    }
};

class Person {
private:
    std::shared_ptr<Bed> bed;
public:
    // Constructor that takes a shared_ptr to a Bed
//    Person(std::shared_ptr<Bed> bedPtr) : bed(bedPtr) {
//        std::cout << "Person created\n";
//    }


    void setSharedBed(const std::shared_ptr<Bed> bedPtr) {
        std::cout<< "setSharedBed bed add:"<<bedPtr<<std::endl;
        bed = bedPtr;
    }
    void setSharedBed2(const std::shared_ptr<Bed> &bedPtr) {
        std::cout<< "setSharedBed bed add:"<<bedPtr<<std::endl;
        bed = bedPtr;
    }



    ~Person() {
        std::cout << "Person destroyed\n";
    }
};

int main() {
    // Create a Bed using a shared_ptr
    //auto bed = std::make_shared<Bed>();
    //std::shared_ptr<Bed> bed =  std::make_shared<Bed>();


    Bed* rawBed = new Bed();
    std::shared_ptr<Bed> bedPtr(rawBed);

    // Create two Persons sharing the same Bed through the shared_ptr
    std::cout<< "bed2 add:"<<bedPtr<<std::endl;
    Person person1;
    person1.setSharedBed(bedPtr);
    std::cout<< "after move bed name: love"<<bedPtr->get_name()<<std::endl;
    Person person2;
    person2.setSharedBed2(bedPtr);



    // No need to manually delete, shared_ptrs will handle it.
}